<?php
error_reporting(E_ALL ^ E_NOTICE);
define('ORDER_SUBMITTED', 10);                 // 针对货到付款而言，他的下一个状态是卖家已发货
define('ORDER_PENDING', 11);                   // 等待买家付款
define('ORDER_ACCEPTED', 20);                  // 买家已付款，等待卖家发货
define('ORDER_SHIPPED', 30);                   // 卖家已发货
define('ORDER_FINISHED', 40);                  // 交易成功
define('ORDER_CANCELED', 0);                   // 交易已取消
define('ORDER_CONFIRM', 50);                   // 交易已取消
define('ROOT_PATH', substr(dirname(__FILE__),0,strlen(dirname(__FILE__))-strlen('service')-1));
include(ROOT_PATH . '/eccore/ecmall.php');

require(ROOT_PATH . '/eccore/model/model.base.php');   //模型基础类
/* 定义配置信息 */
ecm_define(ROOT_PATH . '/data/config.inc.php');
ecm_define(ROOT_PATH . '/data/settings.inc.php');
/* 环境 */
define('CHARSET', 'utf-8');
require_once '../baidupush/sdk.php';
//include_once '../includes/ecapp.base.php';
include_once '../randomchar.php';
include_once 'mobilecommon.php';
include_once 'BaseService.php';
include_once 'WaybillService.php';
include_once 'ShopService.php';
include_once 'SnsService.php';
include_once 'ServiceMechanism.php';
include_once 'MessageService.php';
include_once 'haulCommon.php';

$result = JSON(array('status'=>'参数错误'));
$status=0;
$result=!empty($_POST['result'])?$_POST['result']:"";
$returnurl=!empty($_POST['returnurl'])?$_POST['returnurl']:"";

$db = &db();

// $_POST = (array)json_decode('{"XDEBUG_SESSION_START":"ECLIPSE_DBGP","p":"{\"a_type\":\"1\",\"op\":\"authinfo\",\"action\":\"authinfo\",\"rnd\":\"00000000-2949-7512-ffff-ffffaea4ffb8\",\"uid\":\"13\",\"version\":\"1.9.9\",\"system\":\"android\",\"opdt\":\"20180118160806\",\"gps\":{\"lat\":\"0.000000\",\"lng\":\"0.000000\",\"speed\":\"0.000000\"}}","alert":"alert"}');

$request_uri = addslashes($_SERVER['REQUEST_URI']);
$post_string = addslashes(json_encode($_POST));
//$post_string = $_POST['p'];
$create_time = date('Y-m-d H:i:s');

$sql = "insert into zys_service_access_record(`request_uri`, `post_string`, `create_time`) value ('$request_uri', '$post_string', '$create_time')";

$db->query($sql);
$logid = $db->insert_id();

//$_POST['p'] = '{"row":"0","size":"10","accept":"1","vcode":"沪ED2590","op":"waybilllist","action":"waybilllist","rnd":"ffffffff-9e80-8475-cd35-0d2d0033c587","uid":"383","version":"1.9.8","system":"android","opdt":"20171124105723","gps":{"address":"中国山东省青岛市李沧区枣园路11号","time":"2017-11-24 10:55:42","lat":"36.167873","lng":"120.429999","speed":"0.000000"}}';

try{
	$p = $_POST['p']?$_POST['p']:'';
	if(empty($_POST['alert']))
	{
//		$p=decrypt($p);
	}
	// $p = '{"wmtid":"833","lat":"0.0","lng":"0.0","op":"waybill","action":"waybill","rnd":"ffffffff-d040-abb5-ffff-ffffb7e7f8b9","uid":"54","version":"1.9.8","system":"android","opdt":"20171017151758","gps":{"address":"中国山东省青岛市市北区辽阳西路202号","lat":"36.103994","lng":"120.407579","speed":"0.000000","time":"2017-10-17 15:17:59"}}';

	$p=str_replace('\"','"',$p);
	$p=str_replace("\r",'',$p);
	$p=str_replace("\n",'\n',$p);

	$pars=json_decode($p);

	if(empty($pars)){
		throw new Exception("未取得参数信息");
	}
	$userid = $pars->uid?$pars->uid:'';

	if($userid&&strtolower($pars->op)!='login'){
		$status=-1;
		$a_member=&m("member");
		$userself=$a_member->get("user_id=$userid");
		if(empty($userself)){
			//throw new Exception("用户不存在");
		}
		if($userself['del']){
			//throw new Exception('用户已经禁止使用');
		}
		$sn=$userself['sn'];
		if(empty($sn)||$sn!=$pars->rnd){
			//throw new Exception("用户设备号不对");
		}
		$pars->userself=$userself;
	}
	else{
		$pars->userself=null;
	}
	switch (strtolower($pars->op)) {
		case 'upamin':
			$result=upamin($pars);
			break;
		case 'wtype':
			$result=wtype($pars);
			break;
		case 'boxn':
			$result=boxn($pars);
			break;
		case 'checkbox':
			$result=checkbox($pars);
			break;
		case 'addorderbox':
			$result=addorderbox($pars);
			break;
		case 'orderlistbox':
			$result=orderlistbox($pars);
			break;
		case 'getbox':
            $result=getbox($pars);
            break;
        case 'addbox':
            $result=addbox($pars);
            break;
        case 'add_user':
            $result=add_user($pars);
            break;
        case 'haul_main':
            $result=haul_main($pars);
            break;
        case 'rail_define':
            $result=rail_define($pars);
            break;
        case 'del_haul_main':
            $result=del_haul_main($pars);
            break;
        case 'del_haul_define':
            $result=del_haul_define($pars);
            break;
        case 'del_haul_collect':
            $result=del_haul_collect($pars);
            break;
        case 'del_rail':
            $result=del_rail($pars);
            break;
		case 'finishwaybill':
			$result=finishwaybill($pars);
			break;
		case 'dropwaybill':
			$result=dropwaybill($pars);
			break;
		case 'createwaybillinout':
			$result=createwaybillinout($pars);
			break;
		case 'usecar':
			$result=usecar($pars);
			break;
		case 'order':
			$result=order($pars);
			break;
		case 'update_order':
			$result=update_order($pars);
			break;
		case 'company':
			$result=company($pars);
			break;
		case 'station':
			$result=station($pars);
			break;
		case 'box':
			$result=box($pars);
			break;
		case 'check':
			$result=check($pars);
			break;
		case 'add_order':
			$result=add_order($pars);
			break;
		case 'order_list':
			$result=order_list($pars);
			break;
		case 'authinfo':
			$result=getAuth($pars);
			break;
		case 'auth':
			$result=auth($pars);
			break;
		case 'msgarrive':
			$result=msg_arrive($pars);
			break;
			//通訊錄
		case 'addrbook':
			$result=addrbook($pars);
			break;
			//绑定通道
		case 'bindchannelid':
			$result=bindchannelId($pars);
			break;
        case 'waybillgps':
            $result=waybillgps($pars);
            break;
			//运单列表
		case 'waybilllist':
			$result=waybilllist($pars);
			break;
			//待接运单信息
		case 'unacceptinfo':
			$result=unacceptinfo($pars);
			break;
		case 'waybill':
			$result=getwaybill($pars);
			break;
			//异常列表
		case 'exceptionlist':
		case 'exceptionlsit':
			$result=exceptionlist($pars);
			break;
			//运程确认
		case 'haulsumbit':
			$result=haulSubmit($pars,$_FILES);
			break;
			//异常提报
		case 'exceptionsubmit':
			$file=$_FILES['f']?$_FILES['f']:'';
			$num=$pars->num?$pars->num:0;
			for($i=1;$i<=$num;$i++){
				$f=$_FILES['image'.$i];
				$filelist[]=$f;
			}
			$result=exceptionsubmit($file,$filelist,$pars);
			break;
			
			//运单操作
		case 'waybillsubmit':
				
			$result=waybillsubmit($pars);
			break;
		case 'waybillnum':  //累计运单数量
			$result=waybillnum($pars);
			break;
		case 'waybillfee': //运费列表
			$result=waybillfee($pars);
			break;
		case 'savewaybillfee':  //保存运费
			$result=saveWayBillFee($pars);
			break;
		case 'confirmwaybillfee': //确认结费
			$result=confirmWayBillFee($pars);
			break;
		case 'distance':
			//运程测距
			$result=distance($pars);
			break;
		case 'frontdistance':
			//距下一运程距离
			$result=frontdistance($pars);
			break;
		case 'newsgbill': //创建疏港订单
			$result=createSGWayBill($pars);
			break;
		case 'newjgbill': //创建集港订单
			$result=createJGWayBill($pars);
			break;
		case 'ysplan': //接单
			$result=ysplan($pars);
			break;
			//-------------------------------------------------
			//上传文件
		case 'uploadfile':
			$file = $_FILES['f']?$_FILES['f']:'';
			$result=uploadfile($file,$pars);
			break;
			//异常类别
		case 'exceptiontype':
			$result=getDictionary('exception',$pars);
			break;
			//获取用户信息
		case 'getuserbase':
			$result=getuserbase($pars);
			break;
			//登录
		case 'login':
			$result=login($pars);
			break;
		case 'logout':
			$result=logout($pars);
			break;
			//注册
		case 'reg':
			$account=$pars->t?$pars->t:'';
			$code=$pars->c?$pars->c:'';
			$psw=$pars->p?$pars->p:'';
			$result=reg($account,$code,$psw,$pars);
			break;
			//修改密码
		case 'password':
			$result=password($pars);
			break;
			//设置密码
		case 'getpassword':
			$result=getpassword($pars);
			break;
		case 'vehiclebrandmodel':
			$result=getDictionary('vehiclebrandmodel',$pars);
			break;
		case 'edituser':
			$result=editUser($pars);
			break;
			//获取验证码
		case 'authcode':
			$result=authcode($pars);
			break;
			//验证验证码
		case 'valicode':
			$result=valiAuthcode($pars);
			break;
			//消息列表MessageList
		case 'messagelist':
			$result=MessageList($pars);
			break;
			//消息列表MessageView
		case 'messageview':
			$result=MessageView($pars);
			break;
			//消息列表MessageView
		case 'messagedrop':
			$result=MessageDrop($pars);
			break;
		case 'getdictionary':
			$result=getDictionary($pars);
			break;

			//关于
		case 'didiabout':
			$result=getarticle('about',$pars);
			break;
			//服务协议
		case 'didiprotocol':
			$result=getarticle('service',$pars);
			break;
		case 'contact':
			$result=getarticle('contact',$pars);
			break;
		case 'pt_plateadd':
			$result=pt_plateadd($pars);
			break;
		case 'pt_palletcheckq':
			$result=pt_palletcheckQ($pars);
			break;
		case 'pt_palletcheckd':
			$result=pt_palletcheckD($pars);
			break;
		case 'pt_palletcheckb':
			$result=pt_palletcheckB($pars);
			break;
		default:
			break;
	}
}
catch (Exception $exp){
	$status1=$exp->getMessage();
	$result=JSONResult($status,$status1,$pars);
}

$sql = "update zys_service_access_record set `response`='".addslashes($result)."' where id='$logid'";
$db->query($sql);

if($result=='alert'||!empty($returnurl)){
	if(!empty($returnurl)){
		echo "<form style='display:none;' id='form1' name='form1' method='post' action='$returnurl?XDEBUG_SESSION_START=ECLIPSE_DBGP'>
              <input name='result' type='text' value='{$result}' />
              <input name='r' type='text' value='".$result."'/>
            </form>
            <script type='text/javascript'>function load_submit(){document.form1.submit()}load_submit();</script>";
	}
	else{
		echo('<script>alert(\''.$result.'\');</script>');
	}
}
else{
	header("content-type:text/html;charset=utf-8");
	if(empty($_POST['alert']))
	{
        echo encrypt($result);
	}
	else
	{
		echo $result;
	}
	//echo ($result);
}
?>